!pip install yfinance==0.1.67
!pip install pandas==1.3.3
!pip install requests==2.26.0
!mamba install bs4==4.10.0 -y
!pip install plotly==5.3.1
Collecting yfinance==0.1.67
Downloading yfinance-0.1.67-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: pandas>=0.24 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (1.3.4)
Collecting multitasking>=0.0.7
Downloading multitasking-0.0.11-py3-none-any.whl (8.5 kB)
Requirement already satisfied: numpy>=1.15 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (1.20.3)
Requirement already satisfied: requests>=2.20 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (2.26.0)
Requirement already satisfied: lxml>=4.5.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (4.7.1)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=0.24->yfinance==0.1.67) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=0.24->yfinance==0.1.67) (2021.3)
Requirement already satisfied: six>=1.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance==0.1.67) (1.15.0)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (3.3)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (1.26.7)
Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (2022.6.15)
Installing collected packages: multitasking, yfinance
Successfully installed multitasking-0.0.11 yfinance-0.1.67
Collecting pandas==1.3.3
Downloading pandas-1.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.5 MB)
|████████████████████████████████| 11.5 MB 27.6 MB/s eta 0:00:01
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas==1.3.3) (2.8.2)
Requirement already satisfied: numpy>=1.17.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas==1.3.3) (1.20.3)
Requirement already satisfied: pytz>=2017.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas==1.3.3) (2021.3)
Requirement already satisfied: six>=1.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas==1.3.3) (1.15.0)
Installing collected packages: pandas
Attempting uninstall: pandas
Found existing installation: pandas 1.3.4
Uninstalling pandas-1.3.4:
Successfully uninstalled pandas-1.3.4
Successfully installed pandas-1.3.3
Requirement already satisfied: requests==2.26.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (2.26.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests==2.26.0) (1.26.7)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests==2.26.0) (3.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests==2.26.0) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests==2.26.0) (2022.6.15)
/usr/bin/sh: mamba: command not found
Collecting plotly==5.3.1
Downloading plotly-5.3.1-py2.py3-none-any.whl (23.9 MB)
|████████████████████████████████| 23.9 MB 25.1 MB/s eta 0:00:01
Requirement already satisfied: tenacity>=6.2.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from plotly==5.3.1) (8.0.1)
Requirement already satisfied: six in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from plotly==5.3.1) (1.15.0)
Installing collected packages: plotly
Attempting uninstall: plotly
Found existing installation: plotly 5.1.0
Uninstalling plotly-5.1.0:
Successfully uninstalled plotly-5.1.0
Successfully installed plotly-5.3.1
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.
tesla = yf.Ticker("TSLA")
Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.
tesla_data = tesla.history(period="max")
Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.
tesla_data.reset_index(inplace=True)
tesla_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 | 19.000000 | 25.000000 | 17.540001 | 23.889999 | 18783276.0 | 0 | 0.0 |
| 1 | 2010-06-30 | 25.959999 | 30.419201 | 23.299999 | 23.830000 | 17194394.0 | 0 | 0.0 |
| 2 | 2010-07-01 | 25.000000 | 25.920000 | 20.270000 | 21.959999 | 8216789.0 | 0 | 0.0 |
| 3 | 2010-07-02 | 23.000000 | 23.100000 | 18.709999 | 19.200001 | 5135795.0 | 0 | 0.0 |
| 4 | 2010-07-06 | 20.000000 | 20.000000 | 15.830000 | 16.110001 | 6852634.0 | 0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data.
import requests
url = " https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
html_data = requests.get(url).text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, 'html5lib')
Using BeautifulSoup or the read_html function extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.
tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])
table = soup.find_all("tbody")[1]
rows = table.find_all("tr")
for row in rows:
col = row.find_all("td")
date = col[0].text
revenue = col[1].text
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 48 | 2010-06-30 | $28 |
| 49 | 2010-03-31 | $21 |
| 50 | 2009-12-31 | |
| 51 | 2009-09-30 | $46 |
| 52 | 2009-06-30 | $27 |
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
/tmp/wsuser/ipykernel_166/349343550.py:1: FutureWarning: The default value of regex will change from True to False in a future version.
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
remove an null or empty strings in the Revenue column.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.
tesla_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 47 | 2010-09-30 | 31 |
| 48 | 2010-06-30 | 28 |
| 49 | 2010-03-31 | 21 |
| 51 | 2009-09-30 | 46 |
| 52 | 2009-06-30 | 27 |
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.
game_stop = yf.Ticker("GME")
Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.
gme_data = game_stop.history(period="max")
gme_data.reset_index(inplace=True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 | 1.620128 | 1.693350 | 1.603296 | 1.691667 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 | 1.712707 | 1.716074 | 1.670626 | 1.683250 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 | 1.683250 | 1.687458 | 1.658002 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 | 1.666418 | 1.666418 | 1.578047 | 1.607504 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
import requests
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
html_data = response = requests.get(url).text
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_data, "html5lib")
gme_revenue = pd.DataFrame(columns = ["Date", "Revenue"])
table = soup.find_all("tbody")[1]
rows = table.find_all("tr")
for row in rows:
date = row.find_all("td")[0].text
revenue = row.find_all("td")[1].text
gme_revenue = gme_revenue.append({"Date": date, "Revenue": revenue}, ignore_index=True)
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 57 | 2006-01-31 | $1,667 |
| 58 | 2005-10-31 | $534 |
| 59 | 2005-07-31 | $416 |
| 60 | 2005-04-30 | $475 |
| 61 | 2005-01-31 | $709 |
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"")
gme_revenue.dropna(inplace=True)
gme_revenue = gme_revenue[gme_revenue['Revenue'] != ""]
/tmp/wsuser/ipykernel_166/2914211459.py:1: FutureWarning: The default value of regex will change from True to False in a future version.
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"")
Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 57 | 2006-01-31 | 1667 |
| 58 | 2005-10-31 | 534 |
| 59 | 2005-07-31 | 416 |
| 60 | 2005-04-30 | 475 |
| 61 | 2005-01-31 | 709 |
make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(gme_data, gme_revenue, 'GameStop')